package main

import (
	"context"
	"flag"
	"fmt"
	"github.com/2spmohanty/gosphere/operation"
	"github.com/vmware/govmomi/units"
	"github.com/vmware/govmomi/view"
	"github.com/vmware/govmomi/vim25/mo"
	"log"
)

func main() {
	vc := flag.String("vc", "192.168.7.102", "Enter vCenter IP/ FQDN")
	user := flag.String("user", "administrator@chao.com", "vCenter User")
	pass := flag.String("pass", "15228569Aa.", "Enter vCenter pass")
	flag.Parse()

	fmt.Printf("pass=%s\n", *pass)
	vcenter := operation.NewVCenter(*vc, *user, *pass)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	err := vcenter.Connect(ctx)
	if err != nil {
		fmt.Printf("Failed to connect to vCenter: %s\n", err)
		return
	}

	fmt.Printf("Connected to vCenter: %s\n", *vc)

	// Create view of VirtualMachine objects
	client := vcenter.Client.Client
	manager := view.NewManager(client)

	containerView, err := manager.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
	if err != nil {
		fmt.Printf("Failed to connect to vCenter: %s\n", err)
		return
	}

	// defer containerView.Destroy(ctx)

	// Retrieve summary property for all machines
	// Reference: http://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/vim.VirtualMachine.html
	var vms []mo.VirtualMachine
	err = containerView.Retrieve(ctx, []string{"VirtualMachine"}, []string{"summary"}, &vms)
	if err != nil {
		fmt.Printf("Failed to connect to vCenter: %s\n", err)
		return
	}
	for _, vm := range vms {
		fmt.Printf("虚拟机：%s: %s\n", vm.Summary.Config.Name, vm.Summary.Config.GuestFullName)
	}

	containerView, err = manager.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"HostSystem"}, true)
	var hss []mo.HostSystem
	err = containerView.Retrieve(ctx, []string{"HostSystem"}, []string{"summary"}, &hss)
	if err != nil {
		log.Printf("Failed to connect to vCenter: %s\n", err)
	}

	// Print summary per host (see also: govc/host/info.go)

	fmt.Printf("Name:\tUsed CPU:\tTotal CPU:\tFree CPU:\tUsed Memory:\tTotal Memory:\tFree Memory:\t\n")

	for _, hs := range hss {
		totalCPU := int64(hs.Summary.Hardware.CpuMhz) * int64(hs.Summary.Hardware.NumCpuCores)
		freeCPU := int64(totalCPU) - int64(hs.Summary.QuickStats.OverallCpuUsage)
		freeMemory := int64(hs.Summary.Hardware.MemorySize) - (int64(hs.Summary.QuickStats.OverallMemoryUsage) * 1024 * 1024)
		fmt.Printf("%s\t", hs.Summary.Config.Name)
		fmt.Printf("%d\t", hs.Summary.QuickStats.OverallCpuUsage)
		fmt.Printf("%d\t", totalCPU)
		fmt.Printf("%d\t", freeCPU)
		fmt.Printf("%s\t", (units.ByteSize(hs.Summary.QuickStats.OverallMemoryUsage))*1024*1024)
		fmt.Printf("%s\t", units.ByteSize(hs.Summary.Hardware.MemorySize))
		fmt.Printf("%d\t", freeMemory)
		fmt.Printf("\n")
	}

	fmt.Println("Starting Clone ....")
	//vmops := operation.VMOperation{Context: ctx, Vcenter: vcenter}
	//machine := vms[0]
	//vmMor, rs := vmops.CloneVM("Test-VM-Last", true, &hss[0], &vms[0], &clst, datacenter, &dst)

	/*var vmt mo.VirtualMachine
	err = pc.RetrieveOne(ctx, vmMor.Reference(), nil, &vmt)
	fmt.Println("The New VM is ", vmt.Name)
	fmt.Println("The state of cloning is ", rs)*/

}
